package com.zbkj.admin.controller;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.zbkj.admin.exception.CrmebException;
import com.zbkj.admin.model.user.User;
import com.zbkj.admin.model.user.UserBankAccount;
import com.zbkj.admin.request.UserBankAccountRequest;
import com.zbkj.admin.request.WechatAddressImportRequest;
import com.zbkj.admin.result.CommonResult;
import com.zbkj.admin.service.UserBankAccountService;
import com.zbkj.admin.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * 用户银行卡账号 前端控制器
 *
 * @author llc
 * @since 2025-04-23
 */
@Slf4j
@RestController
@RequestMapping("api/front/userBankAccount")
@Tag(name = "用户 -- 银行卡账号")
public class UserBankAccountController {

    /**
     * 银行卡正则
     */
    private static final String BANK_CARD_REGEX = "^[0-9]{16,19}$";

    @Autowired
    private UserService userService;
    @Autowired
    private UserBankAccountService userBankAccountService;


    @Operation(summary = "用户银行卡账号添加")
    @PostMapping(value = "/add")
    public CommonResult<Integer> add(@RequestBody @Validated UserBankAccountRequest request) {
        // 如果提现类型为银行卡 则校验银行卡是否正常
        if(request.getType() == 2){
            if (!Pattern.matches(BANK_CARD_REGEX, request.getBankCode())) { // 使用正则校验
                throw new CrmebException("银行卡号码格式不正确");
            }
            if (!checkLuhn(request.getBankCode())) {
                throw new CrmebException("银行卡号校验失败");
            }
        }
        // 创建用户银行卡信息
        return CommonResult.success(userBankAccountService.create(request));
    }

    @Operation(summary = "用户银行卡账号编辑")
    @PostMapping(value = "/edit")
    public CommonResult<UserBankAccount> edit(@RequestBody @Validated UserBankAccountRequest request) {
        // 如果提现类型为银行卡 则校验银行卡是否正常
        if(request.getType() == 2){
            if (!Pattern.matches(BANK_CARD_REGEX, request.getBankCode())) { // 使用正则校验
                throw new CrmebException("银行卡号码格式不正确");
            }
            if (!checkLuhn(request.getBankCode())) {
                throw new CrmebException("银行卡号校验失败");
            }
        }

        if (userBankAccountService.edit(request)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    // 校验银行卡
    public static boolean checkLuhn(String cardNo) {
        int sum = 0;
        boolean alternate = false;
        for (int i = cardNo.length() - 1; i >= 0; i--) {
            int n = Integer.parseInt(cardNo.substring(i, i + 1));
            if (alternate) {
                n *= 2;
                if (n > 9) n = (n % 10) + 1;
            }
            sum += n;
            alternate = !alternate;
        }
        return (sum % 10 == 0);
    }


    @Operation(summary = "查询用户银行卡账号列表")
    @GetMapping(value = "/list")
    public CommonResult<List<UserBankAccount>> getList() {
        return CommonResult.success(userBankAccountService.getAllList());
    }

    @Operation(summary = "用户银行卡账号删除")
    @PostMapping(value = "/delete/{id}")
    public CommonResult<String> delete(@PathVariable("id") Integer id) {
        if (userBankAccountService.delete(id)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Operation(summary = "获取银行卡账号详情")
    @GetMapping(value = "/getDetail/{id}")
    public CommonResult<UserBankAccount> getDefault(@PathVariable("id") Integer id) {
        return CommonResult.success(userBankAccountService.getDetail(id));
    }

    @Operation(summary = "获取默认银行卡账号")
    @GetMapping(value = "/get/default")
    public CommonResult<UserBankAccount> getDefault() {
        return CommonResult.success(userBankAccountService.getDefault());
    }

    @Operation(summary = "设置默认银行卡账号")
    @PostMapping(value = "/set/default/{id}")
    public CommonResult<Object> setDefault(@PathVariable(value = "id") Integer id) {
        if (userBankAccountService.setDefault(id)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Operation(summary = "根据类型获取默认银行卡账号")
    @GetMapping(value = "/getDefaultByType/{type}")
    public CommonResult<UserBankAccount> getDefaultByType(@PathVariable(value = "type") String type) {
        return CommonResult.success(userBankAccountService.getDefaultByType(type));
    }
}



